package com.xmut.eduservice.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmut.commonutils.R;
import com.xmut.eduservice.client.VodClient;
import com.xmut.eduservice.entity.EduVideo;
import com.xmut.eduservice.service.EduVideoService;
import com.xmut.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author huangchaoqin
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/eduservice/edu-video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;

    // 添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        boolean b = eduVideoService.save(eduVideo);
        return R.ok();
    }

    // 删除小节
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) {

        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            R result = vodClient.removeVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001, "删除视频失败【熔断器】");
            }
        }
        eduVideoService.removeById(id);
        return R.ok();
    }

    // 修改小节
    @PostMapping("updataVideo")
    public R updataVideo(@RequestBody EduVideo eduVideo) {
        boolean b = eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    // 根据章节id查询
    @GetMapping("getVideoInfo/{videoId}")
    public R getChapterInfo(@PathVariable String videoId) {
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return R.ok().data("video", eduVideo);
    }

}

